/**
 * 判断数据是否为空
 * @param {Array, Object, String} params 数据
 */
export function empty(params) {
    if (params) {
        if (typeof params == 'object') {
            //对象的处理
            if (JSON.stringify(params) == '[]' || JSON.stringify(params) == '{}') {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        return true;
    }
}
/**
 * 将text内容操作到截切版
 * @param {String} execCommand 操作命令，copy是复制，cut是剪切，paste是粘贴
 * @param {String} text 文本
 * @returns 返回粘贴的文本
 */
export function clipboard(execCommand, text) {
    const input = document.createElement('textarea');
    const scorllTop = window.scrollY;
    let t = "";
    document.body.appendChild(input);
    input.focus();
    switch (execCommand) {
        case 'copy' || 'cut':
            input.value = text;
            input.setSelectionRange(0, text.length);
            document.execCommand(execCommand);
            break;
        default:
            document.execCommand('paste');
            t = input.value;
            break;
    }
    input.remove();
    window.scrollTo(0, scorllTop);
    return t;
}

/**
 * 暂停
 * @param {*} timeout 毫秒
 */
export function sleep(timeout) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(timeout);
        }, timeout);
    });

}

/**
 * 分离data与err
 * @param {*} promise 
 */
export function awaitWrap(promise) {
    return promise
        .then(data => ({ err: null, data }))
        .catch(err => ({ err, data: null }))
}

/**
 * 辅助OBJ
 * @param {*} obj 旧OBJ
 */
export function copyObject(obj) {
    return JSON.parse(JSON.stringify(obj));
}

/**
 * 节流
 */
export const timerInterval = (() => {
    let timer = null;

    return (callback) => {
        if (timer) {
            clearInterval(timer);
        }
        timer = setInterval(callback, 1000);
    }
})()

/**
 * 时间格式化
 */

export function unifyDateFromat(now = new Date()) {
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    let date = now.getDate();
    let hours = now.getHours();
    let minutes = now.getMinutes();
    let seconds = now.getSeconds();

    year = numFromat(year, 4);
    month = numFromat(month, 2);
    date = numFromat(date, 2);
    hours = numFromat(hours, 2);
    minutes = numFromat(minutes, 2);
    seconds = numFromat(seconds, 2);

    return (year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds);
}

export function numFromat(num, intd = 0) {
    num = num + "";
    for (let i = num.length; i < intd; i++) {
        num = "0" + num;
    }
    return num;
}
